<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>赋予用户权限</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>
    <meta name="format-detection" content="telephone=no"/>
    <link rel="stylesheet" href="/resource/common/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/resource/css/public.css" media="all"/>
</head>
<style>
    .layui-input {
        width: 130%;
    }
</style>
<body class="index">
<table id="userTables" lay-filter="userTables"></table>
<script type="text/html" id="header">
    <div class="layui-btn-container" style="overflow: hidden">
        <div style="float: right">
            <button class="layui-btn layui-btn-sm" lay-event="addUsers">添加选中</button>
        </div>
    </div>
</script>
<script type="text/javascript" src="/resource/common/layui/layui.js"></script>
<script type="text/javascript" src=" /resource/js/user.js"></script>
<script type="text/javascript" th:inline="javascript">


    layui.use(['table', 'layer', 'laydate', 'form'], function () {
        var table = layui.table,
            $ = layui.$,
            layer = layui.layer,
            form = layui.form
        var roleId = [[${roleId}]];
        var tableIns = table.render({
            elem: '#userTables'
            , cellMinWidth: 100
            , url: '/user/userForRole.do' //数据接口
            , method: 'get'
            , toolbar: '#header'
            , page: true //开启分页
            , id: 'tableId'
            , where: {
                status: 1,
                roleId: roleId
            }
            , limits: [10, 15, 30]
            , limit: 15
            , cols: [
                [ //表头
                    {'type': 'checkbox', 'fixed': 'left'}
                    , {field: 'index', title: '序号'}
                    , {field: 'userName', title: '学工号'}
                    , {field: 'realName', title: '姓名'}
                    , {field: 'createdDate', title: '创建时间'}
                    , {field: 'status', title: '状态'}
                ]
            ]
        });
        table.on('toolbar(userTables)', function (obj) {
            var datas = table.checkStatus("tableId").data;
            if (datas.length == 0) {
                layer.alert("请选择数据后再进行操作！");
            } else {
                var ids = new Array();
                for (d in datas) {
                    ids.push(datas[d].id);
                }
                //添加选中
                if (obj.event == "addUsers") {
                    console.log(ids)
                    addUsers(ids.toString());
                }
            }
        });

        //添加用户函数
        function addUsers(ids) {
            layer.confirm('是否添加?', function (index) {
                $.ajax({
                    url: "/role/addUser.do",
                    type: "post",
                    data: {"ids": ids, "roleId": roleId},
                    dataType: "json",
                    success: function (result) {
                        if (!result.status) {
                            layer.msg(result.msg, {time: 2000});
                            return false;
                        }
                        layer.msg("添加!", {time: 1000});
                        parent.layui.table.reload('tableId', {});
                    }
                });
                parent.layer.close(parent.layer.getFrameIndex(window.name));
                layer.close(index)
            })
        }
    });

</script>

</body>
</html>